//
//  UIButton+Customise.m
//  LaiCai
//
//  Created by SmartMin on 15-8-10.
//  Copyright (c) 2015年 LaiCai. All rights reserved.
//

#import "UIButton+Customise.h"
#import <objc/runtime.h>

static char *buttonCallBackBlockKey;

@implementation UIButton (Customise)

- (void) buttonWithBlock:(void(^)(UIButton *button))buttonClickBlock{
    [self addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    if (buttonClickBlock){
        objc_setAssociatedObject(self, &buttonCallBackBlockKey, buttonClickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}


-(void)buttonClick:(UIButton *)sender{
    void(^buttonClickBlock)(UIButton *button) = objc_getAssociatedObject(sender, &buttonCallBackBlockKey);
    if (buttonClickBlock){
        buttonClickBlock(sender);
    }
}

- (void)layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space
{
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case ButtonEdgeInsetsStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case ButtonEdgeInsetsStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case ButtonEdgeInsetsStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case ButtonEdgeInsetsStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        default:
            break;
    }
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}


#pragma mark - 更新图片 主方法
-(void)uploadMainImageWithURL:(NSString *)urlString placeholder:(UIImage *)placeholder imgType:(PDImgType)type callback:(void(^)(UIImage *image))callbackBlock{
    if ((![urlString isKindOfClass:[NSString class]]) ||(!urlString.length)){
        return;
    }
    // 1. 判断placeholder
    if (placeholder == nil){
        placeholder = [UIImage imageNamed:@"icon_nordata"];
    }
    
    // 2. 判断url
    NSString *mainImgUrl = @"";
    if ([urlString hasPrefix:@"http://"]){
        mainImgUrl = urlString;
    } else {
        mainImgUrl = [NSString stringWithFormat:@"%@%@?x-oss-process=image/resize,m_fill,w_%li,h_%li,limit_0/auto-orient,1/format,png",aliyunOSS_BaseURL,urlString,(long)self.size_width,(long)self.size_height];
    }
    NSURL *imgUrl = [NSURL URLWithString:mainImgUrl];
    
    // 3. 进行加载图片
    [self.imageView sd_setImageWithURL:imgUrl placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (type == PDImgTypeNormal){
            self.imageView.image = image;
        } else if (type == PDImgTypeAvatar){
            self.imageView.image = [Tool imageByComposingImage:image withMaskImage:[UIImage imageNamed:@"round"]];
        }
        
        if (callbackBlock){
            callbackBlock(self.self.imageView.image);
        }
    }];
}




@end
